{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial describes the basics of managing views in Label Studio Data Manager using the Python SDK."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to Label Studio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by connecting to Label Studio using the `Client` module:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-06T12:53:41.802365Z",
     "start_time": "2024-06-06T12:53:41.700580Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from label_studio_sdk import LabelStudio\n",
    "\n",
    "ls = LabelStudio(\n",
    "    base_url=os.getenv('LABEL_STUDIO_URL', 'http://localhost:8080'),\n",
    "    api_key=os.getenv('LABEL_STUDIO_API_KEY')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create view"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can create view for project with predefined filters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-06T12:57:03.784314Z",
     "start_time": "2024-06-06T12:57:03.695218Z"
    }
   },
   "outputs": [],
   "source": [
    "from label_studio_sdk.data_manager import Filters, Column, Operator, Type\n",
    "from datetime import datetime\n",
    "\n",
    "project = ls.projects.create(\n",
    "    title='Project Created from SDK',\n",
    "    label_config='''\n",
    "    <View>\n",
    "    <Text name=\"text\" value=\"$text\"/>\n",
    "    <Choices name=\"sentiment\" toName=\"text\" choice=\"single\" showInLine=\"true\">\n",
    "        <Choice value=\"Positive\"/>\n",
    "        <Choice value=\"Negative\"/>\n",
    "        <Choice value=\"Neutral\"/>\n",
    "    </Choices>\n",
    "    </View>\n",
    "    '''\n",
    ")\n",
    "\n",
    "filters = Filters.create(Filters.OR, [\n",
    "    Filters.item(\n",
    "        Column.id,\n",
    "        Operator.GREATER,\n",
    "        Type.Number,\n",
    "        Filters.value(42)\n",
    "    ),\n",
    "    Filters.item(\n",
    "        Column.completed_at,\n",
    "        Operator.IN,\n",
    "        Type.Datetime,\n",
    "        Filters.value(\n",
    "            datetime(2021, 11, 1),\n",
    "            datetime(2021, 11, 5),\n",
    "        )\n",
    "    )\n",
    "])\n",
    "\n",
    "view = ls.views.create(project=project.id, data={'filters': filters})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List all views"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can list all views of project:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-06T12:57:45.961291Z",
     "start_time": "2024-06-06T12:57:45.930488Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[View(data={'filters': {'conjunction': 'or', 'items': [{'filter': 'filter:tasks:id', 'operator': 'greater', 'type': 'Number', 'value': 42}, {'filter': 'filter:tasks:completed_at', 'operator': 'in', 'type': 'Datetime', 'value': {'max': '2021-11-05T00:00:00.000000Z', 'min': '2021-11-01T00:00:00.000000Z'}}]}}, filter_group=None, id=495690, order=0, ordering=None, project=462633, selected_items=None, user=912)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls.views.list(project=project.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
